<?php
// 如何在一个php中引入另一个php?
@header("Content-Type:text/json;charset=utf-8");
@header("Access-Control-Allow-Origin:*");
$conn = mysqli_connect("b-sjisyki6cozhtk.bch.rds.gz.baidubce.com:3306", "b_sjisyki6cozhtk", "12345678", "b_sjisyki6cozhtk");
if (!$conn) {
    exit('{"status":false,"message":"数据库链接失败"}');
}
mysqli_query($conn, "set names utf8"); // 从数据库取数据时  将编码转为utf-8;  
mysqli_query($conn, "set character set utf-8"); // 向数据库存数据时  将

if (!(isset($_POST["user"]) && isset($_POST["pwd"]))) {
    exit('{"status":false,"message":"请传入完整参数"}');
}
$user = $_POST["user"];
$pwd = $_POST["pwd"];
function isExistPhone($user)
{
    global $conn;
    $sql = "select * from `zhuce` where user = '$user'";
    $result = mysqli_query($conn, $sql);
    $obj = array();
    if ($result) { // 有结果对象 => 可能有数据
        $item = mysqli_fetch_assoc($result);
        // print_r($item === null);
        if (!$item) {
            $obj["status"] = true;
            $obj["message"] = "可以使用的手机号";
        } else {
            $obj["status"] = false;
            $obj["message"] = "手机号已存在!";
        }
    } else {
        $obj["status"] = false;
        $obj["message"] = "sql语句有误";
        $obj["sql"] = $sql;
    }
    return $obj;
}
if (!isExistPhone($user)["status"]) {
    exit(json_encode(isExistPhone($user)));
} else {
    $obj = array();
    $sql = "insert into `zhuce` (user,pwd) values('$user','$pwd')";
    $result = mysqli_query($conn, $sql);
    if ($result) { // true => 语句执行成功
        $rows = mysqli_affected_rows($conn);
        if ($rows > 0) {
            $obj["status"] = true;
            $obj["message"] = "注册成功";
        } else {
            $obj["status"] = false;
            $obj["message"] = "注册失败";
        }
    } else {
        $obj["status"] = false;
        $obj["message"] = "sql语句有误";
        $obj["sql"] = $sql;
    }
    echo json_encode($obj);
}
